Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Is there a way in cypress to accomodate cy.get(`[data-cy="${test}"]`).filter(':visible') and cy.get(`[data-cy="${test}"]`) in a single code?

I have a code like

cy.get(`[data-cy="${test}"]`).filter(':visible').click({force: true})

it works for some tests but for some elements there is no "filter" and those works without .filter(':visible'), so i am trying to accomodate both function in a single code to keep it generic for all kinds of buttons. Is there a way to do it?

I have tried few steps like:

cy.get(`[data-cy="${test}"]`).filter(':visible'|| ':hidden').click({force: true})

not sure whether it is right or wrong but its definielt not working!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are using {force: true} then it doesn't matter if the element is visible or hidden, cypress will click it. So in my opinion you don't need the filter option.

cy.get(`[data-cy="${test}"]`).click({force: true})

Or, If you want to directly apply the OR condition in .filter you can do:

cy.get(`[data-cy="${test}"]`).filter(':visible,:hidden').click({force: true})

Or, If you want to use If-Else you can do something like this:

cy.get(`[data-cy="${test}"]`).then(($ele) => {
  if ($ele.filter(':visible')) {
    cy.wrap($ele).click()
  } else if ($ele.filter(':hidden')) {
    cy.wrap($ele).click({force: true})
  }
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!